﻿Imports Models.Users
Imports Models.Security

Namespace Security

    Public Class Users

        Private loginList As Generic.List(Of UserVO)
        Private groupList As Generic.List(Of GroupVO)


        Sub New()
            groupList = New Generic.List(Of GroupVO)
            loginList = New Generic.List(Of UserVO)

            Dim company As New CompanyVO()
            company.code = "PUBLIC"

            Dim baseRol As New RolVO()
            baseRol.code = "public_user"

            Dim group As GroupVO


            ' Group Default
            group = New GroupVO()
            group.code = "DEFAULT"
            group.name = "Default"
            group.company = company
            groupList.Add(group)


            '' SUPER
            Dim loginVo As New UserVO()
            loginVo.email = "info@mapgeodata.com"
            loginVo.fullName = "Super"
            loginVo.login = "super"
            loginVo.password = "123456"
            loginVo.active = True


            Dim userVo As New UserCompanyVO()
            userVo.groups = groupList
            userVo.company = company
            userVo.rol = baseRol

            loginVo.userCompanies.Add(userVo)

            loginList.Add(loginVo)

        End Sub


        ''' <summary>
        ''' Sincronize companies, groups and users
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub sincronize()

            Using oDB As New Data.BaseModel.BaseModelContainer("name=BaseModelContainer")

                '' add/update groups
                Dim dbGroupList As Generic.List(Of Data.BaseModel.Group) = (From g In oDB.Groups Select g).ToList

                Dim found As Boolean
                For Each group As GroupVO In groupList

                    found = False

                    '' locate user in db
                    For Each dbGroup As Data.BaseModel.Group In dbGroupList
                        If dbGroup.Code = group.code Then
                            dbGroup.Name = group.name
                            dbGroup.System = True
                            found = True
                        End If
                    Next

                    '' create if not found
                    If Not found Then

                        'get company from code
                        Dim entity As New Data.BaseModel.Group()
                        entity.Name = group.name
                        entity.Code = group.code
                        entity.CompanyId = (From c In oDB.Companies Where c.Code = group.company.code Select c.CompanyId).FirstOrDefault
                        entity.System = True


                        oDB.Groups.AddObject(entity)
                    End If
                Next

                '' add/update all users
                Dim dbLoginList As Generic.List(Of Data.BaseModel.User) = (From u In oDB.Users Select u).ToList


                For Each login As UserVO In loginList

                    found = False
                    For Each dbLogin As Data.BaseModel.User In dbLoginList
                        If dbLogin.Login = login.login Then
                            dbLogin.FullName = login.fullName
                            dbLogin.Active = True
                            dbLogin.System = True
                            found = True
                        End If
                    Next

                    If Not found Then

                        Dim entity As New Data.BaseModel.User()

                        entity.FullName = login.fullName
                        entity.Login = login.login
                        entity.Password = login.password
                        entity.Email = login.email
                        entity.Active = login.active
                        entity.System = True

                        For Each user In login.userCompanies

                            Dim entityUser As New Data.BaseModel.UserCompany

                            'get de system company
                            entityUser.CompanyId = (From c In oDB.Companies Where c.Code = user.company.code Select c.CompanyId).FirstOrDefault

                            'get de system rol
                            entityUser.RolId = (From c In oDB.Rols Where c.Code = user.rol.code Select c.RolId).FirstOrDefault

                            entity.UserCompanies.Add(entityUser)
                        Next
                        oDB.Users.AddObject(entity)
                    End If

                Next

                oDB.SaveChanges()

            End Using


        End Sub

    End Class
End Namespace